let app = getApp()
import bodhi_resquest from '../../service/network.js'
Page({
  data: {
    disabled: true, //获取验证码按钮是否禁用
    codename: '获取验证码', //获取验证码按钮名字
    startNum: false,
    user: {
      phone: '',
      password: ''
    },
    carouselData: { //背景轮播图数据
      isFullScreen: true,
      imgSrc: [
        '/static/img/1.jpg',
        '/static/img/2.jpg',
        '/static/img/3.jpg',
        '/static/img/4.jpg'
      ]
    },
    inputConfig: { //输入框配置
      errorMsg: ''
    },
    loginFrameData: { //我的页面 传入登录弹框组件的数据
      frameConfig: { //组件配置
        isOpacity: true, //是否要组件透明
        isNeedMask: false, //是否需要组件遮罩
        isNeedTipText: true, //是否要提示信息
        isShowFrame: true, //是否弹出弹框
        isShowError: false, //是否显示弹出的错误信息
      },
      frameData: { //组件数据
        nickName: '绑定邮箱',
        tip: '注：邮箱账号可作为电脑端登录账户!',
        btnNameLeft: '取消',
        btnNameRight: '绑定',
      }
    }
  },
  //监听邮箱账号输入框输入是否合法
  emailTextBlur(e) {
    var reg = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$")
    let emailStr = e.detail.value.replace(/\s*/g, "")
    if (emailStr == '') {
      this.setData({
        disabled: true,
        'loginFrameData.frameConfig.isShowError': true,
        'inputConfig.errorMsg': '邮箱号不能为空哦！'
      })
      return false;
    }
    if (!reg.test(emailStr)) {
      this.setData({
        disabled: true,
        'loginFrameData.frameConfig.isShowError': true,
        'inputConfig.errorMsg': '邮箱号格式有误哦！'
      })
      return false
    }
    this.data.user.phone = emailStr
    if (this.data.user.phone) {
      this.setData({
        disabled: false
      })
      if (this.data.startNum){
        this.setData({
          disabled: true
        })
      }
    }

  },
  passwordBlur(e) {
    this.data.user.password = e.detail.value
  },
  inputTrigger(e) {
    this.setData({
      'loginFrameData.frameConfig.isShowError': false,
    })
  },
  goMinePage() {
    wx.switchTab({
      url: '/pages/mine/mine'
    })
  },
  goDefineBtn() {
    if (this.data.user.phone == '') {
      this.setData({
        'loginFrameData.frameConfig.isShowError': true,
        'inputConfig.errorMsg': '邮箱账户不能为空！'
      })
      return false
    }
    if (this.data.user.password == '') {
      this.setData({
        'loginFrameData.frameConfig.isShowError': true,
        'inputConfig.errorMsg': '验证码不能为空！'
      })
      return false
    }
    let userid = wx.getStorageSync('userid')
    if (userid) {
      wx.showLoading({
        title: '验证中',
        mask: true
      })
      bodhi_resquest({
        url: 'wx_getcode.php',
        method: "POST",
        data: {
          userid: wx.getStorageSync('userid'),
          email: this.data.user.phone,
          code: this.data.user.password
        },
      }).then((res) => {
        console.log(res)
        if (res.data.status) {
          wx.hideLoading()
          wx.showToast({
            title: res.data.msg,
            mask: true,
            icon: 'success',
            success: () => {
              setTimeout(function() {
                wx.switchTab({
                  url: '/pages/mine/mine'
                })
              }, 1300)
            }
          })
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          return false
        }
      }, (rej) => {
        wx.hideLoading()
        wx.showToast({
          title: '接口调用失败',
          icon: "none"
        })
      })
    } else {
      wx.showToast({
        title: '用户未授权',
        icon: 'none'
      })
    }
  },
  getBindCode() {
    var that = this
    var num = 60;
    that.setData({
      codename: num + "s",
      disabled: true,
      startNum: true
    })
    var timer = setInterval(function() {
      num--;
      if (num <= 0) {
        clearInterval(timer);
        that.setData({
          codename: '重新获取',
          disabled: false,
          startNum: false
        })
      } else {
        that.setData({
          codename: num + "s",
          disabled: true,
          startNum: true
        })
      }
    }, 1000)
    bodhi_resquest({
      url: 'wx_getcode.php',
      method: "POST",
      data: {
        email: this.data.user.phone,
        userid: wx.getStorageSync('userid')
      },
    }).then((res) => {
      if (res.statusCode == 404 || !res.data.status) {
        clearInterval(timer);
        that.setData({
          codename: '获取验证码',
          disabled: false
        })
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    }, (rej) => {
      wx.showToast({
        title: '接口调用失败',
        icon: "none"
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})